const validate = require('../../utils/validate')
const common = require('../../utils/common')
const { queryShopAccountPage } = require('../../services/order')
const defaultPage = {
  curPage: 1,
  pageSize: 10,
}
Page({
  data: {
    total: 10,
    query: {
      ...defaultPage,
      type: '',
    },
    dataList: [],
    loaded: false,
    tabList: [
      { name: '全部', value: '' },
      { name: '收入', value: '1' },
      { name: '支出', value: '2' },
    ],
  },
  onLoad: function () {
    this.queryChange()
  },
  tabChange: function (e) {
    const { index } = e.currentTarget.dataset;
    const item = this.data.tabList[index];
    this.queryChange({ type: item.value, ...defaultPage })
  },
  queryChange: function (payload) {
    this.setData({
      query: { ...this.data.query, ...payload },
    }, this.getDataList)
  },
  getDataList: function () {
    if (this.data.query.curPage > 1 && this.data.dataList.length >= this.data.total) return
    const params = { ...this.data.query }
    queryShopAccountPage(params).then(res => {
      const { total } = res.page
      this.setData({ total })
      if (params.curPage === 1) {
        this.setData({
          dataList: res.data
        })
      } else {
        this.setData({
          dataList: [...this.data.dataList, ...res.data]
        })
      }
      this.setData({
        loaded: true
      })
    }).catch(err => {
      common.log(err)
    })
  },
  scrollBottom: function () {
    const curPage = ++this.data.query.curPage
    this.queryChange({ curPage })
  },
})